package com.banciyuan.bcywebview.biz.detail;

import android.app.Activity;
import android.content.Context;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.TagDetail;

public class CircleLayoutHelper {

    private Context mContext;
    private LayoutInflater layoutInflater;
    private int totalItemWidth;
    private int maxCircleContanierWidth;

    public CircleLayoutHelper(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        totalItemWidth = 0;
        maxCircleContanierWidth = CommonUtils.getWidthPixels((Activity) mContext) - UIUtils.dip2px(94, mContext);
    }

    private boolean avaliableToAppendPic() {
        totalItemWidth += UIUtils.dip2px(30, mContext);
        return (maxCircleContanierWidth - totalItemWidth) > 0;
    }

    private boolean avaliableToAppendText(String name) {
        totalItemWidth += getTextWidth(name, 12) + UIUtils.dip2px(17, mContext);
        return (maxCircleContanierWidth - totalItemWidth) > 0;
    }

    private String splitString(String str) {
        try {
            int byteCount = 0;
            int index = 0;
            for (int i = 0; i < str.length(); i++) {
                Character c = str.charAt(i);
                int length = c.toString().getBytes("GBK").length;
                byteCount += length;
                if (byteCount >= 7) {
                    index = i;
                    break;
                }
            }
            if (index > 0 && index < str.length() - 1)
                return str.substring(0, index + 1) + "...";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }


    private float getTextWidth(String text, int textSize) {
        TextPaint paint = new TextPaint();
        float scaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
        paint.setTextSize(scaledDensity * textSize);
        return paint.measureText(text);
    }

    public void layoutCircleView(ViewGroup circlesContainer, List<TagDetail> tagDetails) {
        totalItemWidth = 0;
        circlesContainer.removeAllViews();
        if (!tagDetails.isEmpty()) {
            for (int i = 0; i < tagDetails.size(); i++) {
                TagDetail tagDetail = tagDetails.get(i);
                if (avaliableToAppendPic()) {
                    View viewChild = layoutInflater.inflate(R.layout.related_circle_item, circlesContainer, false);
                    ImageView ivCirclePic = (ImageView) viewChild.findViewById(R.id.iv_circle_pic);
                    TextView tvCircleName = (TextView) viewChild.findViewById(R.id.tv_circle_name);
                    ImageLoader.getInstance().displayImage(tagDetail.getCover(), ivCirclePic, ImageOptions.options);
                    circlesContainer.addView(viewChild);
                    String splitName;
                    if ("work".equals(tagDetail.getType())) {
                        splitName = splitString(tagDetail.getName());
                    } else {
                        splitName = splitString(tagDetail.getTag_name());
                    }
                    if (avaliableToAppendText(splitName)) {
                        tvCircleName.setText(splitName);
                    } else {
                        tvCircleName.setVisibility(View.GONE);
                        break;
                    }
                } else {
                    break;
                }
            }
        }
    }
}
